package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.utils.CacheUtil;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@Api(tags = "03.公告管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @GetMapping("list")
    @ApiOperation("首页公告列表")
    @ApiOperationSupport(order = 10)
    public JsonResult list(){

        Serializable index=cacheUtil.get("vehicle_index");
        if(index!=null){
            System.out.println("首页数据来自Redis数据库。。。");
            return JsonResult.ok(index);
        }
        List<NoticeListVO> noticeListVOList = noticeMapper.selectNotice();
        cacheUtil.setExpire("vehicle_index",(Serializable) noticeListVOList,30);
        System.out.println("首页数据来自MySQL数据库。。。");
        return JsonResult.ok(noticeListVOList);
    }
}
